package com.yycx.module.bus.provider.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.provider.mapper.BusDrugsPlanMapper;
import com.yycx.module.bus.provider.service.BusDrugsPlanService;
import com.yycx.module.bus.provider.service.BusDrugslotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 药品采购计划表接口实现类
 *
 * @author flyme
 * @date 2020-12-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusDrugsPlanServiceImpl extends BaseServiceImpl<BusDrugsPlanMapper, BusDrugsPlan> implements BusDrugsPlanService {

    @Autowired
    private BusDrugslotService busDrugslotService;
    @Autowired
    private BusDrugsPlanService drugsPlanService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusDrugsPlan busDrugsPlan, EntityMap extra) {
        busDrugsPlan.setCheckState(0);
        BigDecimal drugsNum = busDrugsPlan.getDrugsNum();
        BigDecimal costPrice = busDrugsPlan.getCostPrice();
        BigDecimal planAmount = drugsNum.multiply(costPrice);
        BigDecimal advancePayment = busDrugsPlan.getAdvancePayment();
        busDrugsPlan.setPlanAmount(planAmount);
        BigDecimal returnAmount = planAmount.subtract(advancePayment);
        busDrugsPlan.setReturnAmount(returnAmount);
        Long userId = OpenHelper.getUserId();
        busDrugsPlan.setUserId(userId);
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusDrugsPlan> cq, BusDrugsPlan bdp, EntityMap requestMap) {
        cq.select(BusDrugsPlan.class, "*");
        cq.select(BusDrugs.class, "drugsName");
        cq.createJoin(BusDrugs.class).setMainField("drugsId").setJoinField("drugsId");
        cq.orderByDesc("bdp.createTime");
        return ResultBody.ok();
    }

    @Override
    public Boolean updateBusDrugsPlanCheckState(Long drugsPlanId, String processInstanceId, Integer checkState) {
        CriteriaUpdate cu = new CriteriaUpdate();
        cu.set(true, "checkState", checkState);
        cu.set(true, "processInstanceId", processInstanceId);
        cu.eq(true, "drugsPlanId", drugsPlanId);
        return update(cu);
    }

    @Override
    public EntityMap findByBusDrugsPlanId(String drugsPlanId) {
        CriteriaQuery cq = new CriteriaQuery(BusDrugsPlan.class);
        cq.select(BusDrugsPlan.class, "*");
        cq.select(BusPaymenttype.class, "paymentTypeName", "paymentTypeCode");
        cq.select(BusDept.class, "deptName");
        cq.select(BusDrugs.class, "drugsName");
        cq.eq(BusDrugsPlan.class, "drugsPlanId", drugsPlanId);
        cq.createJoin(BusDept.class).setMainField("deptId").setJoinField("deptId");
        cq.createJoin(BusDrugs.class).setMainField("drugsId").setJoinField("drugsId");
        cq.createJoin(BusPaymenttype.class).setMainField("paymentTypeId").setJoinField("paymentTypeId");
        return findOne(cq);
    }

    @Override
    public ResultBody batchSaveDrugsLot(Map params) {
        Long drugsId = TypeUtils.castToLong(params.get("drugsId"));
        BigDecimal costPrice = TypeUtils.castToBigDecimal(params.get("costPrice"));
        String drugsLotInfo = (String) params.get("drugsLotInfo");
        Long drugsPlanId= TypeUtils.castToLong(params.get("drugsPlanId"));
        JSONArray jsonArray = JSON.parseArray(drugsLotInfo);
        List<BusDrugslot> busDrugsLotList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String drugsLotNo = jsonObject.getString("drugsLotNo");
            Integer drugsNum = jsonObject.getInteger("drugsNum");
            BusDrugslot busDrugslot = new BusDrugslot();
            busDrugslot.setDrugsId(drugsId);
            busDrugslot.setCostPrice(costPrice);
            busDrugslot.setDrugsLotNo(drugsLotNo);
            busDrugslot.setDrugsNum(drugsNum);
            busDrugslot.setCreateTime(DateUtils.getNowDateTime());
            busDrugsLotList.add(busDrugslot);
        }
        BusDrugsPlan drugsPlan=drugsPlanService.getById(drugsPlanId);
        //已入库
        drugsPlan.setCheckState(9);
        drugsPlanService.saveOrUpdate(drugsPlan);
        if (busDrugsLotList.size() > 0) {
            boolean b = busDrugslotService.saveBatch(busDrugsLotList);
            if (b) {
                return ResultBody.ok();
            }
        }
        return ResultBody.failed();
    }

}
